<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 300px;
            height: 100px;
            margin: 0 auto;
        }

        .box2 {
            width: 300px;
            height: 100px;
            margin: 0 auto;
        }

        .left1 {
            margin-right: 30px;
            margin-left: -30px;
        }
    </style>
</head>

<body>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <div id="app">
        <div class="box1">
            <button @click="fn" class="button1">切换显示与隐藏</button>
            <div v-show="flag">
                <p>{{num1}}+{{num2}}={{num1+num2}}</p>
                <button @click="num1++" class="left1">num1+1</button>
                <button @click="num2+=2">num2+2</button>
            </div>
        </div>
        <hr>
        <div class="box2">
            <div>
                <h3>售货机</h3>
                <button @click="buy(5)" class="left1">可乐5元</button>
                <button @click="buy(10)">雪碧10元</button>
            </div>
            <div>
                <h3>库存</h3>
                <span class="left1">可乐：{{Num1}}瓶</span>
                <span>雪碧：{{Num2}}瓶</span>
                <div>银行卡余额:{{money}}</div>
            </div>
        </div>
    </div>
    <script>
        const { createApp } = Vue
        createApp({
            data() {
                return {
                    num1: 0,
                    num2: 0,
                    Num1: 10,
                    Num2: 15,
                    money: 100,
                    flag: true
                }
            },
            methods: {
                fn() {
                    this.flag = !this.flag
                },
                buy(n) {

                    if (this.Num1 === 0 || this.Num2 === 0) {
                        alert('库存不足')
                        return
                    }
                    if (this.money - n < 0) {
                        alert('余额不足')
                        return
                    }
                    if (n === 5) {
                        this.Num1--
                    }
                    else {
                        this.Num2--
                    }
                    this.money -= n
                }
            }
        }).mount('#app')
    </script>
</body>

</html>